package com.comtime.manager;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

import com.comtime.smartbox.MainActivity;
import com.comtime.smartkilimanjaro.R;

public class SKNotificationManager {

	private static NotificationManager notificationManager;
	private static int notification_id;

	private static PowerManager pm;
	private static PowerManager.WakeLock wl;

	public static void showNotification(Context context, String ticker,
			String contentText, int value, boolean needVibrate,
			int notificationId) {
		int result = 15000;
		try {
			result = Settings.System.getInt((context).getContentResolver(),
					Settings.System.SCREEN_OFF_TIMEOUT);
		} catch (SettingNotFoundException e) {

			e.printStackTrace();
		}

		// ��ȡ��Դ����������
		pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

		// ��ȡPowerManager.WakeLock���󣬺���Ĳ���|��ʾͬʱ��������ֵ�������ǵ����õ�Tag
		wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
				| PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");

		// ������Ļ
		wl.acquire(result);

		if (notificationManager == null) {
			notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		Notification notification = new Notification();
		notification.icon = R.drawable.ic_launcher;
		notification_id = notificationId;
		notification.tickerText = ticker;
		notification.defaults = Notification.DEFAULT_SOUND;
		notificationManager.cancel(notificationId);// 先移除

		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setClass(context,MainActivity.class);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);

		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);// notification
															// 默认打开主页面

		switch (value) {

		case 10:// 行李到达
			notification.flags = Notification.FLAG_AUTO_CANCEL;
			break;
		case 11:
			notification.flags = Notification.FLAG_AUTO_CANCEL;
			break;
		
		default:
			break;
		}
		if (needVibrate){
			notification.vibrate = new long[] { 50, 500, 50, 500 };
		}
		//高版本中无法使用，待解决

		notification.setLatestEventInfo(context,
				context.getText(R.string.app_name), notification.tickerText,
				pendingIntent);


		notificationManager.notify(notification_id, notification);

	}

	/**
     *  
     */
	public static void cancelNofication(int id) {

		if (notificationManager != null) {
			notificationManager.cancel(id);
			notificationManager = null;
		}
	}

	public static void cancelAll() {
		if (notificationManager != null) {
			notificationManager.cancelAll();
		}
	}
}
